# Contributor: Bartłomiej Piotrowski <bpiotrowski@alpinelinux.org>
# Contributor: Rasmus Thomsen <oss@cogitri.dev>
# Maintainer:
pkgname=lldb
# Note: Update together with llvm.
pkgver=17.0.6
pkgrel=0
_llvmver=${pkgver%%.*}
pkgdesc="Next generation, high-performance debugger"
# riscv64: https://github.com/llvm/llvm-project/issues/55383
arch="all !riscv64"
url="https://llvm.org/"
license="Apache-2.0"
depends_dev="$pkgname=$pkgver-r$pkgrel"
makedepends="
	clang-dev>=$_llvmver
	clang-static>=$_llvmver
	cmake
	doxygen
	gtest-dev
	libedit-dev
	libffi-dev
	libxml2-dev
	linux-headers
	llvm$_llvmver-dev
	llvm$_llvmver-gtest
	llvm$_llvmver-static
	ncurses-dev
	py3-sphinx
	python3-dev
	samurai
	swig
	xz
	"
subpackages="$pkgname-dev $pkgname-doc py3-$pkgname-pyc py3-$pkgname:py3"
source="https://github.com/llvm/llvm-project/releases/download/llvmorg-${pkgver//_/-}/lldb-${pkgver//_/}.src.tar.xz
	https://github.com/llvm/llvm-project/releases/download/llvmorg-${pkgver//_/-}/cmake-${pkgver//_/}.src.tar.xz
	atomic.patch
	fix-embedded_interpreter.patch
	"
builddir="$srcdir/$pkgname-${pkgver//_/}.src"
options="!check"  # FIXME: enable tests

prepare() {
	default_prepare
	mv "$srcdir"/cmake-${pkgver//_/}.src "$srcdir"/cmake
}

build() {
	export CC=clang
	export CXX=clang++

	case "$CARCH" in
	arm*)
		# binutils bugs
		;;
	*)
		export CFLAGS="$CFLAGS -flto"
		export CXXFLAGS="$CXXFLAGS -flto"
		;;
	esac

	cmake -B build -G Ninja -Wno-dev \
		-DCMAKE_BUILD_TYPE=Release \
		-DCMAKE_INSTALL_PREFIX=/usr \
		-DCMAKE_SKIP_RPATH=ON \
		-DLLDB_BUILT_STANDALONE=ON \
		-DLLDB_INCLUDE_TESTS="$(want_check && echo ON || echo OFF)" \
		-DLLVM_ENABLE_SPHINX=ON \
		-DLLVM_LINK_LLVM_DYLIB=ON \
		-DCMAKE_POSITION_INDEPENDENT_CODE=ON

	cmake --build build --target all docs-lldb-man
}

check() {
	ninja -C build check-lldb
}

package() {
	# backtracing/debugging pretty much anything is going to need libc debug symbols
	# not everything, but the common case needs this, and it's a small dep in comparison
	depends="musl-dbg"

	DESTDIR="$pkgdir" cmake --install build
	install -Dm644 build/docs/man/lldb.1 -t "$pkgdir"/usr/share/man/man1
	python3 -m compileall -fq "$pkgdir"/usr/lib/python3*
}

dev() {
	default_dev

	# move back main symlink, because py3-lldb links to it
	mv "$subpkgdir"/usr/lib/liblldb.so "$pkgdir"/usr/lib/
}

py3() {
	pkgdesc="Python3 module for LLDB"
	depends="$pkgname=$pkgver-r$pkgrel python3"
	replaces="py-lldb"

	amove usr/lib/python3.*
}

sha512sums="
80b327c89b160b1dcdbea1946a7c69514e797056629c0a7501c77e7557d6bb3a7ae6ecdd0d760d072b1f7c3f5b136838f76c8d75522cd84622a03b1b25ba6e6c  lldb-17.0.6.src.tar.xz
b2c5e404ca36542d44e1a7f2801bbcecbcf5f1e8c63b793bb2308228483406bdfe002720aadb913c0228cd2bbe5998465eaadc4a49fad6a5eb6ff907fa5fd45a  cmake-17.0.6.src.tar.xz
6493c9bbcaf3f99ffbb56967147716bc93aa13b6c525214abc22723162bf42d6c25663ed1e2e11f432a8c1b246a528149e240eae027ce752bc20cc928ed57272  atomic.patch
fcd3f06b5f901d635bfe8edffa3951d5af402738ee55b13a98c992725eb35db17b7a3ca566eb94d48cd699be899f7402ae41da0d2f1ed4611afc5858a0eec561  fix-embedded_interpreter.patch
"
